// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcTextureState.h"

MgcImplementRTTI(MgcTextureState,MgcRenderState);
MgcImplementStream(MgcTextureState);
MgcImplementDefaultState(MgcTextureState);

//----------------------------------------------------------------------------
MgcRenderState::Type MgcTextureState::GetType () const
{
    return RS_TEXTURE;
}
//----------------------------------------------------------------------------
MgcTextureState::MgcTextureState ()
{
}
//----------------------------------------------------------------------------
MgcTextureState::~MgcTextureState ()
{
    RemoveAll();
}
//----------------------------------------------------------------------------
void MgcTextureState::Update (MgcRenderState*& rpkOldState)
{
    // Textures get replaced on an element-by-element basis, not on an
    // array-by-array basis.  The default texture state is used for storage
    // of the replaced values.

    assert( rpkOldState == ms_spkDefault );
    if ( rpkOldState != ms_spkDefault )
        return;

    for (unsigned int uiIndex = 0; uiIndex < MAX_TEXTURES; uiIndex++)
    {
        // save old state for later restoring
        MgcTexture* pkSave = ms_spkDefault->Get(uiIndex);
        ms_spkDefault->Set(uiIndex,m_aspkTexture[uiIndex]);
        m_aspkTexture[uiIndex] = pkSave;
    }
}
//----------------------------------------------------------------------------
void MgcTextureState::Restore (MgcRenderState* pkOldState)
{
    // This texture state might have replaced textures in the default texture
    // state during the accumulation phase.  They need to be restored.

    assert( pkOldState == ms_spkDefault );
    if ( pkOldState != ms_spkDefault )
        return;

    for (unsigned int uiIndex = 0; uiIndex < MAX_TEXTURES; uiIndex++)
    {
        // restore the old state
        MgcTexture* pkSave = ms_spkDefault->Get(uiIndex);
        ms_spkDefault->Set(uiIndex,m_aspkTexture[uiIndex]);
        m_aspkTexture[uiIndex] = pkSave;
    }
}
//----------------------------------------------------------------------------
MgcRenderState* MgcTextureState::Copy ()
{
    MgcTextureState* pkState = new MgcTextureState;
    for (unsigned int uiIndex = 0; uiIndex < MAX_TEXTURES; uiIndex++)
        pkState->m_aspkTexture[uiIndex] = m_aspkTexture[uiIndex];

    return pkState;
}
//----------------------------------------------------------------------------
void MgcTextureState::Set (unsigned int uiIndex, MgcTexture* pkTexture)
{
    assert( uiIndex < MAX_TEXTURES );
    if ( uiIndex >= MAX_TEXTURES )
        return;

    m_aspkTexture[uiIndex] = pkTexture;
}
//----------------------------------------------------------------------------
MgcTexture* MgcTextureState::Get (unsigned int uiIndex)
{
    assert( uiIndex < MAX_TEXTURES );
    if ( uiIndex >= MAX_TEXTURES )
        return 0;

    return m_aspkTexture[uiIndex];
}
//----------------------------------------------------------------------------
MgcTexturePtr MgcTextureState::Remove (unsigned int uiIndex)
{
    assert( uiIndex < MAX_TEXTURES );
    if ( uiIndex >= MAX_TEXTURES )
        return 0;

    MgcTexturePtr spSave = m_aspkTexture[uiIndex];
    m_aspkTexture[uiIndex] = 0;
    return spSave;
}
//----------------------------------------------------------------------------
void MgcTextureState::RemoveAll ()
{
    for (unsigned int uiIndex = 0; uiIndex < MAX_TEXTURES; uiIndex++)
        m_aspkTexture[uiIndex] = 0;
}
//----------------------------------------------------------------------------
unsigned int MgcTextureState::GetQuantity () const
{
    unsigned int uiQuantity = 0;

    for (unsigned int uiIndex = 0; uiIndex < MAX_TEXTURES; uiIndex++)
    {
        if ( m_aspkTexture[uiIndex] )
            uiQuantity++;
    }

    return uiQuantity;
}
//----------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcTextureState::Factory (MgcStream& rkStream)
{
    MgcTextureState* pkObject = new MgcTextureState;
    MgcStream::Link* pkLink = new MgcStream::Link(pkObject);
    pkObject->Load(rkStream,pkLink);
    return pkObject;
}
//---------------------------------------------------------------------------
void MgcTextureState::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcRenderState::Load(rkStream,pkLink);

    // link data
    for (unsigned int uiI = 0; uiI < MAX_TEXTURES; uiI++)
    {
        MgcTexture* pkTexture;
        MgcStreamRead(rkStream,pkTexture);
        pkLink->Add(pkTexture);
    }
}
//---------------------------------------------------------------------------
void MgcTextureState::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcRenderState::Link(rkStream,pkLink);

    for (unsigned int uiI = 0; uiI < MAX_TEXTURES; uiI++)
    {
        MgcObject* pkLinkID = pkLink->GetLinkID();
        m_aspkTexture[uiI] = (MgcTexture*)rkStream.GetFromMap(pkLinkID);
    }
}
//---------------------------------------------------------------------------
bool MgcTextureState::Register (MgcStream& rkStream)
{
    if ( !MgcRenderState::Register(rkStream) )
        return false;

    for (unsigned int uiI = 0; uiI < MAX_TEXTURES; uiI++)
    {
        if ( m_aspkTexture[uiI] )
            m_aspkTexture[uiI]->Register(rkStream);
    }

    return true;
}
//---------------------------------------------------------------------------
void MgcTextureState::Save (MgcStream& rkStream)
{
    MgcRenderState::Save(rkStream);

    // link data
    for (unsigned int uiI = 0; uiI < MAX_TEXTURES; uiI++)
        MgcStreamWrite(rkStream,m_aspkTexture[uiI]);
}
//---------------------------------------------------------------------------
